import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.GridBagLayout;
import java.awt.Image;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.util.Properties;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.swing.ButtonModel;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.ScrollPaneConstants;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;


public class AdminNotification extends JPanel{
	JTextArea text;
	JButton submit,home;
	JPanel panel;
	Image img;
	
	public AdminNotification(){
		img=new ImageIcon("admin_notification_background.png").getImage();
		setOpaque(false);
		setBackground(Color.WHITE);
		panel=new JPanel();
		panel.setBackground(Color.WHITE);
		panel.setPreferredSize(new Dimension(600,400));
		panel.setLayout(new BorderLayout());
		panel.setBackground(Color.white);
		panel.setOpaque(false);
		
		text=new JTextArea();
		text.setPreferredSize(new Dimension(500,300));
		JLabel t=new JLabel("Enter text to be sent to the students:");
		t.setPreferredSize(new Dimension(500,50));
		
		
		int v2 = ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS; 
		int h2 = ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER;
		JScrollPane scroll=new JScrollPane(text,v2,h2);//,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
		scroll.setMinimumSize(new Dimension(500,300));
		scroll.setPreferredSize(new Dimension(500,300));
		
		JPanel p1=new JPanel();
		p1.setLayout(new GridBagLayout());
		p1.add(scroll);
		
		
		JPanel b=new JPanel();
		b.setOpaque(false);
		//b.setLayout(new GridLayout(2,1));
		b.add(t);
		b.add(scroll);
		
		
		submit=new JButton();
		home=new JButton();
		JPanel bp=new JPanel();
		bp.setPreferredSize(new Dimension(400,40));
		//bp.setLayout(new GridLayout(1,2));
		bp.add(submit);
		bp.add(home);
		bp.setOpaque(false);
		
		 final ImageIcon ho=new ImageIcon("home_button.png");
	  		final ImageIcon cho=new ImageIcon("home_cbutton.png");
	  		final ImageIcon su=new ImageIcon("submit_button.png");
	  		final ImageIcon csu=new ImageIcon("submit_cbutton.png");
	  		button(submit,su);
			button(home,ho);
		
		
		home.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
               
                remove(panel);
                add(new AdminMenuPanel(), BorderLayout.PAGE_START);
                validate();
                repaint();
               
            }
    });
		 home.setContentAreaFilled(false);
         home.setIcon(ho);
         home.setRolloverIcon(cho);
         home.setPressedIcon(cho);
         home.getModel().addChangeListener(new ChangeListener() {
 		        @Override
 		        public void stateChanged(ChangeEvent e) {
 		            ButtonModel model = (ButtonModel) e.getSource();
 		            if (model.isRollover()) {
 		            	home.setIcon(cho);
 		            } else {
 		            	home.setIcon(ho);
 		            }// etc ...//  if (model.isPressed()) {
 		         }
 		    });
		
		submit.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
            	sendEmail();
            	JOptionPane.showMessageDialog(null, "You've successfully sent the email!");
            }
    });
		
		 submit.setContentAreaFilled(false);
		 submit.setIcon(su);
		 submit.setRolloverIcon(csu);
		 submit.setPressedIcon(csu);
		 submit.getModel().addChangeListener(new ChangeListener() {
 		        @Override
 		        public void stateChanged(ChangeEvent e) {
 		            ButtonModel model = (ButtonModel) e.getSource();
 		            if (model.isRollover()) {
 		            	submit.setIcon(csu);
 		            } else {
 		            	submit.setIcon(su);
 		            }// etc ...//  if (model.isPressed()) {
 		         }
 		    });
		JPanel p2=new JPanel();
		p2.setLayout(new GridBagLayout());
		p2.add(bp);
		p2.setOpaque(false);
		
		
		
		panel.add(b, BorderLayout.CENTER);
		panel.add(p2,BorderLayout.SOUTH);
		
		setLayout(new BorderLayout());
		add(panel,BorderLayout.PAGE_START);
		
	}
	
	private void sendEmail(){
		//load the username(email) of the all students and send email to all of them.
		 String userid="";
		 String mailSmtpHost = "localhost";
		 String mailTo,mailFrom,mailSubject,mailText;
		 mailFrom = "admin@troy.edu";
 		mailSubject = "Important Notice From Troy University Office";
        try{
                String dName = "com.mysql.jdbc.Driver";
                String url = "jdbc:mysql://db4free.net:3306/studentreg14?user=studentreg14&password=KCTSkcts";
                Connection con = DriverManager.getConnection(url);
               
                java.sql.Statement mysqlStatement = con.createStatement();
                java.sql.ResultSet resultSet = mysqlStatement.executeQuery("SELECT student_id FROM student_list");
               
                while (resultSet.next()) {
                	userid=resultSet.getString(1);
                	

            		mailTo = userid+"@studentreg14.com";
            		mailText = text.getText();

            		sendEmail(mailTo, mailFrom, mailSubject, mailText, mailSmtpHost);
                }
                con.close();
                }
                catch(Exception ex){}
		
	}
	
	public static void sendEmail(String to, String from, String subject, String text, String smtpHost) {
		try {
			Properties properties = new Properties();
			properties.put("mail.smtp.host", smtpHost);
			Session emailSession = Session.getDefaultInstance(properties);

			Message emailMessage = new MimeMessage(emailSession);
			emailMessage.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
			emailMessage.setFrom(new InternetAddress(from));
			emailMessage.setSubject(subject);
			emailMessage.setText(text);

			emailSession.setDebug(true);

			Transport.send(emailMessage);
		} catch (AddressException e) {
			e.printStackTrace();
		} catch (MessagingException e) {
			e.printStackTrace();
		}
	}

	private void button(JButton b, ImageIcon i){
		b.setBorderPainted(false);
		b.setBorder(null);
		//button.setFocusable(false);
		b.setMargin(new Insets(0, 0, 0, 0));
		b.setContentAreaFilled(false);
		b.setIcon(i);
		//b.setRolloverIcon(myIcon2);
		//b.setPressedIcon(myIcon3);
	}
	
	@Override
    public void paintComponent(Graphics g) {
		super.paintComponents(g);
        g.drawImage(img, 0, 0, this);
    }
}
